package com.vrv.netmgr.entry;

import com.vrv.common.UpdateDialog;
import com.vrv.common.UpdateDialog.MyDialogInt;
import com.vrv.netmgr.R;
import com.vrv.netmgr.persistent.LoginSharedPreferencesHelper;
import com.vrv.netmgr.upgrade.CheckVersion;
import com.vrv.netmgr.vo.LoginInfo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 描述：系统设置页面
 * @author yzj
 */
public class SystemSetting extends Activity {
	// 定义组件
	private LoginInfo loginInfo;
	private ImageView autoLogin, fullScreen, checkVersion, aboutFun, cancelLogin;
	//版本检测处理类
	private CheckVersion mCheckVersion;
	//消息处理
	private final int UPDATE_SELECT = 0x100;
	private final int UPDATE_INFO = 0x101;
	private Handler mHandler;
	//等待对话框
	private ProgressDialog progressDialog;
	//
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.system_setting);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_title);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		//初始化页面
		initViews();
		//设置标题
		setTitle();
		//设置事件监听
		addEventListener();
		//消息处理
		mHandler = new Handler(){
			//
			public void handleMessage(Message msg) {
				//
				progressDialog.dismiss();
				//
	            switch (msg.what) {
	            case UPDATE_SELECT:
	                doSelect();
	            case UPDATE_INFO:
	            	Toast.makeText(SystemSetting.this, "已经是最新版本", Toast.LENGTH_SHORT).show();
	            default:
	                break;
	            }
	        }
		};
	}
	/**
	 * 描述：初始化组件
	 */
	public void initViews(){
		// 初始化登录信息
		loginInfo = LoginSharedPreferencesHelper.getLoginInfo(getApplicationContext());
		//
		autoLogin = (ImageView) findViewById(R.id.image_autoLogin);
		fullScreen = (ImageView) findViewById(R.id.image_fullScreen);
		checkVersion = (ImageView) findViewById(R.id.image_checkVersion);
		aboutFun = (ImageView) findViewById(R.id.image_aboutFun);
		cancelLogin = (ImageView) findViewById(R.id.image_cancelLogin);
		//检查自动登录信息
		if(loginInfo.isAutoLogin()){
			autoLogin.setImageResource(R.drawable.setting_image_open);
		}else{
			autoLogin.setImageResource(R.drawable.setting_image_close);
		}
		//检查全屏设置信息
		if(loginInfo.isFullScreen()){
			fullScreen.setImageResource(R.drawable.setting_image_open);
		}else{
			fullScreen.setImageResource(R.drawable.setting_image_close);
		}
	}
	/**
	 * 描述：实现事件监听
	 */
	public void addEventListener(){
		//自动登录监听
		autoLogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(loginInfo.isAutoLogin()){
					autoLogin.setImageResource(R.drawable.setting_image_close);
					LoginSharedPreferencesHelper.saveLoginInfo(getApplicationContext(), populateLoginInfo("autoLogin"));
				}else{
					autoLogin.setImageResource(R.drawable.setting_image_open);
					LoginSharedPreferencesHelper.saveLoginInfo(getApplicationContext(), populateLoginInfo("autoLogin"));
				}
			}
		});
		//全屏设置监听
		fullScreen.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(loginInfo.isFullScreen()){
					fullScreen.setImageResource(R.drawable.setting_image_close);
					LoginSharedPreferencesHelper.saveLoginInfo(getApplicationContext(), populateLoginInfo("fullScreen"));
				}else{
					fullScreen.setImageResource(R.drawable.setting_image_open);
					LoginSharedPreferencesHelper.saveLoginInfo(getApplicationContext(), populateLoginInfo("fullScreen"));
				}
			}
		});
		//版本检查监听
		checkVersion.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				checkUpgrade();
			}
		});
		//关于功能监听
		aboutFun.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(SystemSetting.this, SystemSettingFun.class));
			}
		});
		//注销登录监听
		cancelLogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				LoginSharedPreferencesHelper.saveLoginInfo(getApplicationContext(), populateLoginInfo("cancelLogin"));
				//发送广播，关闭指定关闭的页面
				sendBroadcast(new Intent().setAction("exitActivity"));
				SystemSetting.this.finish();
				startActivity(new Intent(SystemSetting.this, LoginActivity.class));
			}
		});
	}
	/**
	 * 描述：存储系统设置信息
	 */
	private LoginInfo populateLoginInfo(String s){
		//自动登录信息
		if(s.equals("autoLogin")){
			if(loginInfo.isAutoLogin()){
				loginInfo.setAutoLogin(false);
				loginInfo.setRememberPasswd(true);
			}else{
				loginInfo.setAutoLogin(true);
				loginInfo.setRememberPasswd(true);
			}
		}
		//设置全屏信息
		if(s.equals("fullScreen")){
			if(loginInfo.isFullScreen()){
				loginInfo.setFullScreen(false);
//				Toast.makeText(SystemSetting.this, "退出全屏", Toast.LENGTH_LONG).show();
			}else{
				loginInfo.setFullScreen(true);
//				Toast.makeText(SystemSetting.this, "开启全屏", Toast.LENGTH_LONG).show();
			}
		}
		//注销登录
		if(s.equals("cancelLogin")){
			loginInfo.setRememberPasswd(false);
			loginInfo.setAutoLogin(false);
			loginInfo.setLoginName(null);
			loginInfo.setLoginPasswd(null);
		}
		return loginInfo;
	}
	/**
	 * 描述：设置标题内容
	 */
	public void setTitle(){
		//
		TextView title = (TextView) findViewById(R.id.title_text);
		title.setText("系统设置");
		// 初始化返回按钮
		ImageButton backBtn = (ImageButton) findViewById(R.id.backBtn);
		backBtn.setVisibility(View.VISIBLE);
		backBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}
	@Override
	public void onBackPressed() {
		finish();
		overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
		super.onBackPressed();
	}
	/**
	 * 描述：检测更新最新版本
	 */
	public void checkUpgrade(){
		//
		progressDialog = ProgressDialog.show(SystemSetting.this, "", "检查中...", true, true);
		//
		new Thread(){
			public void run(){
				mCheckVersion = new CheckVersion(SystemSetting.this, mHandler);
				int local = mCheckVersion.getLocalVersionCode();
                int remote = mCheckVersion.getRemoteVersionCode();
                if (remote < 0) {
                    return;
                }
                boolean b = mCheckVersion.compareVersion(local, remote);
                Message msg = new Message();
                if (b) {
                    msg.what = UPDATE_SELECT;
                    mHandler.sendMessage(msg);
                }else{
                	msg.what = UPDATE_INFO;
                	mHandler.sendMessage(msg);
                }
			}
		}.start();
	}
	/**
	 * 描述：选择升级或取消
	 */
	private void doSelect(){
		final UpdateDialog ud = new UpdateDialog(SystemSetting.this);
        ud.setLeftButton("确定", new MyDialogInt() {
			@Override
			public void onClick(View view) {
				ud.dismiss();
				String file = mCheckVersion.getApkName();
				mCheckVersion.download(file, null);
			}
		});
        ud.setRightButton("取消", new MyDialogInt() {
			@Override
			public void onClick(View view) {
				ud.dismiss();
			}
		});
	}
}
